import { defineStore } from 'pinia'
import $ajax from '@/ajax/index.js'

const controller = '/todo'

const useTodoStore = defineStore('todo', {
  // /q/find/all
  state: () => {
    return {
      /** @type {{ todoid: number, ttile: string, info: string ,todotime:Date , finishedtime:string,isFinished: string }[]} */
      todoList: [
        {
          todoid: 1,
          title: '打卡',
          info: '每天早8点需要打卡',
          todotime: new Date(),
          fulltime: new Date(),
          benintime: new Date(),
          endtime: new Date(),
          status: 'finished'
        }
      ],
      todo: {
        todoid: 1,
        title: '打卡',
        info: '每天早8点需要打卡',
        todotime: null,
        fulltime: new Date(),
        benintime: null,
        endtime: null,
        status: '未开始'
      },
      /** @type {'all' | 'unstarted' | 'finished' | 'unfinished'} */
      filter: 'all',
      /** @type { dialogFormVisible: boolean} */
      dialogFormVisible: false,
      index: -1
    }
  },
  getters: {
    finishedTodos(state) {
      return state.todoList.filter((todo) => todo.isFinished === 'finished')
    },
    unfinishedTodos(state) {
      return state.todoList.filter((todo) => todo.isFinished === 'unfinished')
    },

    unstartedTodos(state) {
      return state.todoList.filter((todo) => todo.isFinished === 'unstarted')
    },

    filteredTodos(state) {
      if (state.filter === 'finished') {
        return this.finishedTodos
      } else if (state.filter === 'unfinished') {
        return this.unfinishedTodos
      } else if (state.filter === 'unstarted') {
        return this.unstartedTodos
      } else {
        return this.todoList
      }
    }
  },
  actions: {
    async findAll() {
      const path = controller + '/q/find/all' // http://localhost:8080/todo/q/find/all
      const { data: res } = await $ajax.get(path)
      console.log(res)

      if (res.code === 200) {
        // this.todoList.splice(0, this.todoList.length, res.result)
        this.todoList = res.result
      } else {
        alert('error ')
      }
    },

    preEditItem(index) {
      this.index = index
      this.todo = JSON.parse(JSON.stringify(this.todoList[index]))
      this.dialogFormVisible = true
    },
    async deleteItem(index) {
      const todo = this.todoList[index]
      const path = controller + '/m/delete/' + todo.todoid
      const { data: res } = await $ajax.delete(path)
      console.log(res)

      // this.todoList.splice(index, 1) //   index
    },
    async startItem(index) {
      const todo = this.todoList[index]
      const path = controller + '/m/start/' + todo.todoid
      const { data: res } = await $ajax.put(path)
      if (res.code === 200) {
        todo.status = '进行中'
        todo.benintime = new Date()
      }

      // const todo = this.todoList[index]
    },
    async finishItem(index) {
      const todo = this.todoList[index]
      const path = controller + '/m/finish/' + todo.todoid
      const { data: res } = await $ajax.put(path)
      if (res.code === 200) {
        todo.status = '已完成'
        todo.endtime = new Date()
      }
      // todo.isFinished = true
      // todo.finishedtime = new Date()
    },
    preAddNewItem() {
      this.todo = JSON.parse(JSON.stringify(this.todo))
      this.todo.todoid = -1
      this.dialogFormVisible = true
    },
    doConfirm() {
      if (this.todo.todoid === -1) {
        this.addNewItem()
      } else {
        this.doEditItem()
      }
      this.dialogFormVisible = false
    },
    doEditItem() {
      this.todoList.splice(this.index, 1, this.todo)
    },

    async addNewItem() {
      const path = controller + '/m/add/todo'
      const { data: res } = await $ajax.post(path, this.todo)
      if (res.code === 200) {
        this.todo.todoid = res.result
        this.todoList.push(this.todo)
      }
    }
  }
})

export default useTodoStore
